﻿using System;
using System.Collections.Generic;

using System.Text;

namespace Corsat.Library.Logic
{
    public class Account
    {
        public int AccountId { get; set; }
        public string ArabicName { get; set; }

        public string SSN { get; set; }
        public Library.Item Diocese { get; set; }
        public Library.Item Church { get; set; }
        public Library.Item Study { get; set; }
        public Library.Item Profession { get; set; }
        public string Email { get; set; }
        public DateTime BirthDate { get; set; }

        public bool IsOnlineRegistered { get; set; }

        public bool IsActivated { get; set; }

        public List<Telephone> Telephones { get; set; }

        public int CorsatId { get; set; }

        public int ConferenceStatusId { get; set; }
        public DateTime? ConferenceBookingDate { get; set; }
        public DateTime? ConferenceCompeleteBookingDate { get; set; }

        public List<AccountCourse> Courses = new List<AccountCourse>();

        public AccountTechnologyCourse TechnologyCourse { get; set; }

        public Account()
        {

        }

        public Account(int AccountId)
        {
            try
            {
                Account account = DataManager.Main.GetAccountById(AccountId);

                AccountId = account.AccountId;
                IsOnlineRegistered = account.IsOnlineRegistered;
                ArabicName = account.ArabicName;
                Email = account.Email;
                SSN = account.SSN;
                BirthDate = account.BirthDate;
                CorsatId = account.CorsatId;

                Church = account.Church;
                Diocese = account.Diocese;

                Courses = account.Courses;
                Telephones = account.Telephones;

                TechnologyCourse = account.TechnologyCourse;
            }
            catch (Exception exc)
            {

            }
        }

        public void Save()
        {
            if (AccountId == 0)
                AccountId = DataManager.Main.InsertAccount(this);
        }

        public int Register(List<ChosenCourse> Courses)
        {
            try
            {
                return DataManager.Main.RegisterAccount(this, Courses);
            }
            catch (Exception exc)
            {
                return 0;
            }
        }

        public static bool Activate(int AccountId)
        {
            try
            {
                return DataManager.Main.ActivateAccount(AccountId);
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static bool ValidateCoursePrequists(string SSN, int CourseTitleId, int CourseLevelId)
        {
            try
            {
                int lastYear = DateTime.Now.Year - 1;
                List<int> passedCourses = DataManager.Main.GetPassedCourses(SSN, lastYear);
                List<int> previousPassedCourses = DataManager.Main.GetPassedCourses(SSN, lastYear-1);
                foreach (int previousPassedCourseId in previousPassedCourses)
                {
                    if (!passedCourses.Contains(previousPassedCourseId))
                    {
                        passedCourses.Add(previousPassedCourseId);
                    }
                }
                int CourseId = DataManager.Main.GetCourseId(CourseTitleId, CourseLevelId);

                if (passedCourses.Count == 0 || CourseId == 0) return false;

                switch (CourseId)
                {
                    case 2:
                        if (passedCourses.Contains(1)) return true;
                        break;

                    case 7:
                        if (passedCourses.Contains(5) || passedCourses.Contains(6)) return true;
                        break;

                    case 9:
                        if (passedCourses.Contains(8)) return true;
                        break;

                    case 12:
                        if (passedCourses.Contains(4) || passedCourses.Contains(11)) return true;
                        break;

                    case 14:
                        if (passedCourses.Contains(13)) return true;
                        break;

                    case 16:
                        if (passedCourses.Contains(15)) return true;
                        break;

                    case 21:
                        if (passedCourses.Contains(20) || passedCourses.Contains(3)) return true;
                        break;
                }

                return false;
            }
            catch (Exception exc)
            {
                return false;
            }
        }


        #region Printing
        public string FirstCourse
        {
            get
            {
                return Courses != null && Courses.Count > 0 ? Courses[0].FullName : string.Empty;
            }
        }

        public string SecondCourse
        {
            get
            {
                return Courses != null && Courses.Count > 1 ? Courses[1].FullName : string.Empty;
            }
        }

        public string ThirdCourse
        {
            get
            {
                return Courses != null && Courses.Count > 2 ? Courses[2].FullName : string.Empty;
            }
        }

        #endregion
    }
}
